home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
nurbsTrim
/
nurbsTrim.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
7KB
|
230 lines
/*
* Copyright (C) 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* Clicking any mouse button toggles variable useTrim; when True, the
* nurbs surface is trimmed with the nurbs curve; when False, trim is
* not done. On my workstation, an Indigo 2 Extreme running Irix 5.2,
* attempting to trim the nurbs curve causes it do disappear altogether.
* This suggests that bug #210400, "NURBS Trim Curve Bug", resides in
* OpenGL, not in OpenInventor.
*/
#include <stdio.h>
#include <stdlib.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
static int sglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
Display * dpy;
Window win;
GLboolean doubleBuffer = GL_TRUE;
GLUnurbsObj * NurbsSrf;
void
fatalError( char *message )
{
fprintf(stderr, "NurbsProfile: %s\n", message);
exit(1);
}
void
redraw( GLboolean useTrim )
{
const GLint uSize = 10;
const GLint vSize = 9;
const static GLfloat uKnotVector [10] =
{ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9. };
const static GLfloat vKnotVector [9] =
{ 2., 2., 2., 2., 3., 4., 4., 4., 4. };
const GLint uStride = 15;
const GLint vStride = 3;
const static GLfloat sCP [6][5][3] = {
{ {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0}, {4.0, 1.,4.0} },
{ {4.0, 1.,4.0}, {3.5, 1.,3.1}, {5.0, 1.,4.0}, {3.5, 1.,4.9}, {3.5, 1.,3.1} },
{ {5.0, 1.,4.0}, {3.5, 1.,4.9}, {2.4, 0.,1.3}, {7.5, 0.,4.0}, {2.4, 0.,6.7} },
{ {2.4, 0.,1.3}, {7.2, 0.,4.0}, {2.4, 0.,6.7}, {3.5,-1.,3.1}, {5.0,-1.,4.0} },
{ {3.5,-1.,4.9}, {3.5,-1.,3.1}, {5.0,-1.,4.0}, {3.5,-1.,4.9}, {4.0,-1.,4.0} },
{ {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0}, {4.0,-1.,4.0} }
};
const GLint uOrder = 4;
const GLint vOrder = 4;
const GLint cSize = 9;
const static GLfloat cKnotVector [9] =
{ 0., 0., 1., 2., 3., 4., 5., 6., 6. };
const GLint cStride = 2;
const static GLfloat cCP [7][2] = {
{5.00, 3.83}, /* a */
{4.82, 3.78}, /* b */
{4.25, 3.64}, /* c */
{4.11, 3.26}, /* d */
{5.00, 3.16}, /* e */
{4.81, 3.52}, /* f */
{5.00, 3.83} /* a */
};
const GLint cOrder = 2;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
gluBeginSurface( NurbsSrf );
gluNurbsSurface( NurbsSrf, uSize, (GLfloat *)uKnotVector,
vSize, (GLfloat *)vKnotVector, uStride, vStride,
(GLfloat *)sCP, uOrder, vOrder, GL_MAP2_VERTEX_3 );
if ( useTrim )
{
gluBeginTrim( NurbsSrf );
gluNurbsCurve( NurbsSrf, cSize, (GLfloat *)cKnotVector,
cStride, (GLfloat *)cCP, cOrder, GLU_MAP1_TRIM_2 );
/*
gluPwlCurve( NurbsSrf, cSize-2, (GLfloat *)cCP,
cStride, GLU_MAP1_TRIM_2 );
*/
gluEndTrim( NurbsSrf );
}
gluEndSurface( NurbsSrf );
if ( doubleBuffer )
glXSwapBuffers( dpy, win ); /* implicit glFlush */
else
glFlush( ); /* explicit flush for single buffered case */
}
void
main( int argc, char ** argv )
{
XVisualInfo * vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needRedraw = GL_FALSE;
GLboolean recalcModelView = GL_TRUE;
GLfloat distance = 8.;
GLboolean useTrim = GL_FALSE;
GLfloat xAngle = 12.0;
GLfloat yAngle = 82.0;
GLfloat zAngle = 112.0;
int dummy;
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
fatalError("could not open display");
if(!glXQueryExtension(dpy, &dummy, &dummy))
fatalError("X server has no OpenGL GLX extension");
/* find an OpenGL-capable RGB visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
if (vi == NULL)
{
vi = glXChooseVisual(dpy, DefaultScreen(dpy), sglBuf);
if (vi == NULL)
fatalError("no RGB visual with depth buffer");
doubleBuffer = GL_FALSE;
}
if(vi->class != TrueColor)
fatalError("TrueColor visual required for this program");
/* create an OpenGL rendering context */
cx = glXCreateContext( dpy, vi, None, GL_TRUE );
if (cx == NULL)
fatalError("could not create rendering context");
/* create an X colormap since probably not using default visual */
cmap = XCreateColormap( dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone );
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow( dpy, RootWindow(dpy, vi->screen), 0, 0,
300, 300, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa );
XSetStandardProperties( dpy, win, "NurbsProfile", "NurbsProfile",
None, argv, argc, NULL);
/* bind the rendering context to the window */
glXMakeCurrent( dpy, win, cx );
XMapWindow( dpy, win );
/* configure the OpenGL context for rendering */
glEnable( GL_DEPTH_TEST ); /* enable depth buffering */
glEnable( GL_AUTO_NORMAL ); /* & automatic normal calculation */
glClearColor(0.1, 0.1, 0.3, 0.0);
/* set up projection transform */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
/* create NURBS object */
NurbsSrf = gluNewNurbsRenderer( );
gluNurbsProperty( NurbsSrf, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON );
/* dispatch X events */
while (1)
{
do
{
XNextEvent( dpy, &event );
switch ( event.type )
{
case ButtonPress:
recalcModelView = GL_TRUE;
useTrim = useTrim ? GL_FALSE : GL_TRUE;
break;
case ConfigureNotify:
glViewport(0, 0, event.xconfigure.width,
event.xconfigure.height);
needRedraw = GL_TRUE;
break;
case Expose:
needRedraw = GL_TRUE;
break;
}
}
while(XPending(dpy)); /* loop to compress events */
if (recalcModelView)
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); /* set modelview to identity matrix */
/* move the camera to where you can see something */
glTranslatef( 0.0, 0.0, -distance );
/* rotate by X, Y, and Z angles */
glRotatef( xAngle, 0.1, 0.0, 0.0 );
glRotatef( yAngle, 0.0, 0.1, 0.0 );
glRotatef( zAngle, 0.0, 0.0, 1.0 );
recalcModelView = GL_FALSE;
needRedraw = GL_TRUE;
}
if (needRedraw)
{
redraw( useTrim );
needRedraw = GL_FALSE;
}
}
}